#!/usr/bin/env groovy

def call() {
  pipeline {
    agent {
      node {
        label 'brokersview'
      }
    }

    environment {
      // sDate=time()
      app_name='brokersview-web'
      dockerfile="Dockerfile.test"
      dockerdir='./'
      // dockerregistry='https://553972031073.dkr.ecr.ap-east-1.amazonaws.com'
      dockerregistry='https://registry.cn-shenzhen.aliyuncs.com'
      docker_image='registry.cn-shenzhen.aliyuncs.com/stl-test/brokersview'
      startDate=timeForm()

      data_agent=dataAgent()
      envtag=imageTag()

      docker_image_tag="${env.app_name}-${env.envtag}"
      docker_key='fb805ed9-7e7e-4c79-a9e4-325d5afacc7a'
    }
    stages {
      stage('删除工作空间') {
        steps {
          script{
            deleteDir()
          }
        }
      }
      stage('拉取git仓库') {
        failFast true
        parallel {
          stage('拉取git仓库2') {
            environment {
              git_url='ssh://git@gitwbp5.tostar.top/Bailun/brokers-view-web.git'
              git_key='92b42e30-38f2-442d-a2c2-9763fb14d6bf'
              dev_tag=varEnv.branchTag(BRANCH)
              project='code_directory'
            }
            steps {
              sshagent(credentials: [git_key], ignoreMissing: true) {
                dir("${env.WORKSPACE}") {
                  sh '''
                               git clone --depth 1 --branch=${dev_tag} ${git_url} ${WORKSPACE}/${project}
                     '''
                }
              }
            }
          }
        }
      }
      stage('build构建镜像') {
        environment {
          project='code_directory'
          docker_builddir="${env.WORKSPACE}/${env.project}"
        }
        steps {
          dir("${env.docker_builddir}"){
            script {
              try {
                withDockerRegistry(credentialsId: docker_key, url: dockerregistry) {
                  def customImage = docker.build("${docker_image}:${docker_image_tag}", "-f ${dockerfile} ${dockerdir}")
                  customImage.push()
                }
              }
              catch(exc){
                currentBuild.result='FAILURE'
                echo'构建失败'
//                err_message()
                sh'exit 4'
              }
            }
          }
        }
      }

      stage('关联发布') {
        when {
          allOf{
            expression {
              return  env.release_dev == 'true';
            }
          }
        }
        steps {
          build job: 'test-brokersview-web-update', parameters: [
                  string(name: 'release_tag', value: env.docker_image_tag)
          ]
        }
      }
      // stage('关联发布') {
      //   environment {
      //     endDate=timeForm()
      //   }
      //   when {
      //     allOf{
      //       expression {
      //         return  isNotice == 'true';
      //       }
      //     }
      //
      //   }
      //   steps {
		  //       echo "aaaa"
      //     //message()
      //   }
      // }
    }
  }

}
